
import { useState, useCallback, useEffect } from "react";

import { message } from "antd";
import { queryPhotoList, removePhoto } from "@/api/content/attachment";
import { Attachment } from "@/types/content/attachment";
export const usePhotoAlbum = () => {
    const [messageApi] = message.useMessage();
    const [loading, setLoading] = useState(false);
    const [searchForm, setSearchForm] = useState<Attachment.QueryForm>({
        type: "0",
        tag: ''
    })
    const [pagination, setPagination] = useState<Pagination>({
        pageNo: 1,
        pageSize: 10,
        total: 0,
    });
    const [tableData, setTableData] = useState<Attachment.Row[]>([]);
    /**
     * 获取标签列表
     */
    const getTableList = useCallback(async () => {
        const { tag, type } = searchForm
        const params = {
            // 页码
            pageNo: pagination.pageNo || 1,
            pageSize: pagination.pageSize || 10,
            type,
            tag,
        };
        setLoading(true);
        const res = await queryPhotoList(params);
        setLoading(false);
        if (res.code === 200) {
            setTableData(res.data?.rows || []);
            setPagination({
                pageNo: pagination.pageNo,
                pageSize: pagination.pageSize,
                total: res.data.total
            })

        }
    }, [pagination.pageNo, pagination.pageSize, searchForm]);
    /**
  * 删除评论
  * @param id 
  */
    const removePhotoAlbum = async (row: Attachment.Row) => {
        setLoading(true)
        const res = await removePhoto(row.id)
        setLoading(false)
        if (res.code === 200) {
            messageApi.success('删除成功')
            if (tableData.length === 1) {
                setPagination({
                    pageNo: Math.max(1, pagination.pageNo - 1),
                    pageSize: pagination.pageSize,
                    total: pagination.total
                })
            } else {
                getTableList()
            }
        }
    }
    useEffect(() => {
        getTableList();
    }, [getTableList]);
    return {
        loading, tableData, pagination, setPagination, removePhotoAlbum, setSearchForm, getTableList
    }
}

